//
//  WJBaseButton.m
//  WJBSApp
//
//  Created by dllo on 16/7/13.
//  Copyright © 2016年 王景伟. All rights reserved.
//

#import "WJBaseButton.h"

@interface WJBaseButton ()
@property (nonatomic, retain) UIColor *dayBgColor;
@property (nonatomic, retain) UIColor *nightBgColor;
@property (nonatomic, retain) UIColor *dayTextColor;
@property (nonatomic, retain) UIColor *nightTextColor;
@end

@implementation WJBaseButton

// 会走下面的 buttonWithType 方法
+ (instancetype)buttonWithTitle:(NSString *)title frame:(CGRect)frame target:(id)target action:(SEL)action {
    WJBaseButton *button = [WJBaseButton buttonWithType:UIButtonTypeCustom];
    button.frame = frame;
    [button setTitle:title forState:UIControlStateNormal];
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    return button;
}

+ (instancetype)buttonWithTitle:(NSString *)title frame:(CGRect)frame target:(id)target action:(SEL)action dayBgColor:(UIColor *)dayBgColor dayTextColor:(UIColor *)dayTextColor nightBgColor:(UIColor *)nightBgColor nightTextColor:(UIColor *)nightTextColor {
    WJBaseButton *button = [WJBaseButton buttonWithType:UIButtonTypeCustom];
    button.nightBgColor = nightBgColor;
    button.nightTextColor = nightTextColor;
    button.dayBgColor = dayBgColor;
    button.dayTextColor = dayTextColor;
    button.frame = frame;
    [button setTitle:title forState:UIControlStateNormal];
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    [button changeColor];
    return button;
}

+ (instancetype)buttonWithType:(UIButtonType)buttonType dayBgColor:(UIColor *)dayBgColor dayTextColor:(UIColor *)dayTextColor nightBgColor:(UIColor *)nightBgColor nightTextColor:(UIColor *)nightTextColor {
    WJBaseButton *button = [super buttonWithType:buttonType];
    button.nightBgColor = nightBgColor;
    button.nightTextColor = nightTextColor;
    button.dayBgColor = dayBgColor;
    button.dayTextColor = dayTextColor;
    [button changeColor];
    [[NSNotificationCenter defaultCenter] addObserver:button selector:@selector(changeColor) name:@"changeColor" object:nil];
    return button;
}

+ (instancetype)buttonWithType:(UIButtonType)buttonType {
    WJBaseButton *baseButton = [super buttonWithType:buttonType];
    baseButton.nightBgColor = FIRBLACKCOLOR;
    baseButton.nightTextColor = BSRedColor;
    baseButton.dayBgColor = LABLECOLOR;
    baseButton.dayTextColor = BSRedColor;
    [baseButton changeColor];
    [[NSNotificationCenter defaultCenter] addObserver:baseButton selector:@selector(changeColor) name:@"changeColor" object:nil];
    return baseButton;
}

//通知方法
- (void)changeColor {
    if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"Mode"] isEqualToString:@"night"]) {
        if(self.nightBgColor) self.backgroundColor = self.nightBgColor;
        if(self.nightTextColor) [self setTitleColor:self.nightTextColor forState:UIControlStateNormal];
    } else {
        if(self.dayBgColor) self.backgroundColor = self.dayBgColor;
        if(self.dayTextColor) [self setTitleColor:self.dayTextColor forState:UIControlStateNormal];
    }
}

//重写dealloc 删除通知
- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"changeColor" object:nil];
}


- (CGRect)titleRectForContentRect:(CGRect)contentRect {
    if (!CGRectIsEmpty(self.titleRect) && !CGRectEqualToRect(self.titleRect, CGRectZero)) {
        return self.titleRect;
    }
    return [super titleRectForContentRect:contentRect];
}


- (CGRect)imageRectForContentRect:(CGRect)contentRect {
    if (!CGRectIsEmpty(self.imageRect)&&!CGRectEqualToRect(self.imageRect, CGRectZero)) {
        return self.imageRect;
    }    
    return [super imageRectForContentRect:contentRect];
}







@end
